const http=require("http");
const fs=require("fs");
const template=require("art-template");
const path=require("path");

const tempHtml={
    user:path.join(__dirname,"../template/user.html"),
    list:path.join(__dirname,"../template/list.html"),
    list2:path.join(__dirname,"../template/list2.html")
};

let user={
    id:1011,
    name:"张三",
    phone:"15898745698"
};
let users=[
    {
        id:1001,
        name:"张三",
        phone:"15898745697",
        flag:true
    },
    {
        id:1002,
        name:"张三",
        phone:"15898745695",
        flag:false
    },
    {
        id:1003,
        name:"张三",
        phone:"15898745694",
        flag:false
    },
    {
        id:1004,
        name:"张三",
        phone:"15898745693",
        flag:true
    },
    {
        id:1005,
        name:"张三",
        phone:"15898745692",
        flag:true
    },
    {
        id:1006,
        name:"张三",
        phone:"15898745691",
        flag:true
    }

];

const findUser=function(){
    let temp=fs.readFileSync(tempHtml.user).toString();
    temp=temp.replace("{{id}}",user.id);
    temp=temp.replace("{{name}}",user.name);
    temp=temp.replace("{{phone}}",user.phone);
    return temp;
};
const findList=function(){
    let listTemp="";
    for(let u of users){
        listTemp+=`
            <tr>
                <td>${u.id}</td>
                <td>${u.name}</td>
                <td>${u.phone}</td>
                <td>${u.flag?"生病":"健康"}</td>
            </tr>

        `;
    }
  
    let temp=fs.readFileSync(tempHtml.list).toString();
    temp=temp.replace("{{list}}",listTemp);
    return temp;
};
const findList2=function(){
    let temp=fs.readFileSync(tempHtml.list2).toString();
    let strs=template.render(temp,{"users":users});
    return strs;
};
module.exports={
    "findUser":findUser,
    "findList":findList,
    "findList2":findList2
};